package com.zhongge.bintree;

import com.sun.source.tree.Tree;

/**
 * @ClassName MyBinTree
 * @Description TODO 自定义二叉树
 * @Author zhongge
 * @Date 2025/9/7 1:45
 * @Version 1.0
 */
public class MyBinTree {

    //二叉树的数据结构的实现
    public static class TreeNode{
        //数值域
        public char val;
        //左孩子
        public TreeNode left;
        //右孩子
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //各种方法
    public TreeNode createNode() {
        //创建各个节点
        TreeNode a = new TreeNode('a');
        TreeNode b = new TreeNode('b');
        TreeNode c = new TreeNode('c');
        TreeNode d = new TreeNode('d');
        TreeNode e = new TreeNode('e');
        TreeNode f = new TreeNode('f');
        TreeNode g = new TreeNode('g');
        TreeNode h = new TreeNode('h');
        a.left = b;
        b.left = d;
        b.right = e;
        e.right = h;
        a.right = c;
        c.left = f;
        c.right = g;

        return a;
    }

    //////////////////////////////////////////////////////
    //二叉树的遍历

    /**
     * 递归实现前序遍历
     * @param root 根节点
     */
    public void preOrder(TreeNode root) {
        //递归出口
        if (root == null) {
            return;
        }
        //递归剪枝:无
        //递归核心代码
        System.out.print(root.val + " ");
        //递归的递
        //调用左边的递归
        preOrder(root.left);
        //调用右边的递归
        preOrder(root.right);
        //递归的归
        //return;
    }

}
